package com.geotmt.billingcenter.modules.consume.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.geotmt.billingcenter.common.entry.ConsumeBaseHisEntry;
import com.geotmt.billingcenter.common.enumpackage.ConsumeFeeFlagEnum;
import com.geotmt.billingcenter.common.enumpackage.RuleLabelEnum;
import com.geotmt.billingcenter.common.vo.consume.ConsumeHisVo;
import com.geotmt.billingcenter.common.vo.consume.ConsumeVo;
import com.geotmt.billingcenter.common.vo.params.ParamsConsume;
import com.geotmt.billingcenter.common.vo.params.ParamsForDel;
import com.geotmt.billingcenter.modules.consume.mapper.ConsumeBaseHisMapper;
import com.geotmt.billingcenter.modules.consume.service.ConsumeBaseHisService;
import com.geotmt.billingcenter.modules.rules.service.RulesService;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Description 类描述
 * @Author      yanghanwei
 * @Mail        yanghanwei@geotmt.com
 * @Date        2019/3/13 14:44
 * @Version     v1
 **/
@Service
@Transactional(rollbackFor = Exception.class)
public class ConsumeBaseHisServiceImpl implements ConsumeBaseHisService {

    private static final Logger logger = LoggerFactory.getLogger(ConsumeBaseHisServiceImpl.class);

    @Resource
    private ConsumeBaseHisMapper consumeBaseHisMapper;

    @Resource
    private RulesService rulesService;

    @Override
    public Integer addBaseHis(ConsumeBaseHisEntry consumeBaseHis) {
        logger.info("消耗 his add Base service");
        consumeBaseHisMapper.addBaseHis(consumeBaseHis);
        return consumeBaseHis.getId();
    }

    @Override
    public void deleteBaseHis(ParamsForDel obj) {
        logger.info("消耗 his delete Base service");
        consumeBaseHisMapper.deleteBaseHis(obj);
    }

    @Override
    public List<ConsumeHisVo> listHis(ParamsConsume obj) {
        Integer pageNum = obj.getPageNum();
        Integer pageSize = obj.getPageSize();
        String orderBy = obj.getOrderBy();
        if(null != pageNum && null != pageSize){
            PageHelper.startPage(pageNum,pageSize);
        }
        if(!StringUtils.isEmpty(orderBy)){
            PageHelper.orderBy(orderBy);
        }
        List<ConsumeHisVo> consumeHisVos = consumeBaseHisMapper.listHis(obj);
        //查询 规则的详细计费规则(阶梯)
        if(null != consumeHisVos){
            for(ConsumeHisVo consumeVo : consumeHisVos){
                if(ConsumeFeeFlagEnum.STAIR.value.equals(consumeVo.getFeeFlag())){
                    consumeVo.setStairRuleList(rulesService.stairListGetByRid(consumeVo.getRId(), RuleLabelEnum.CONSUME_HIS.value));
                }
            }
        }
        return consumeHisVos;
    }

    @Override
    public void updateEndEffect(Date preDay, Integer id) {
        logger.info("更新有效结束时间");
        consumeBaseHisMapper.updateEndEffect(preDay, id);
    }

}
